package com.bybops.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.bybops.app.MyApplication;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

public class SPUtils {
    public SPUtils() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * 17      * 保存在手机里面的文件名
     * 18
     */
    public static final String FILE_NAME = "CloudServer";

    /**
     * 22      * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
     * 23      *
     * 24      * @param context
     * 25      * @param key
     * 26      * @param object
     * 27
     */
    public static void put(String key, Object object) {

        SharedPreferences sp = MyApplication.Companion.getInstance().getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();

        if (object instanceof String) {
            editor.putString(key, (String) object);
        } else if (object instanceof Integer) {
            editor.putInt(key, (Integer) object);
        } else if (object instanceof Boolean) {
            editor.putBoolean(key, (Boolean) object);
        } else if (object instanceof Float) {
            editor.putFloat(key, (Float) object);
        } else if (object instanceof Long) {
            editor.putLong(key, (Long) object);
        } else {
            editor.putString(key, object.toString());
        }

        SharedPreferencesCompat.apply(editor);
    }

    /**
     * 59      * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
     * 60      *
     * 61      * @param context
     * 62      * @param key
     * 63      * @param defaultObject
     * 64      * @return
     * 65
     */
    public static Object get(String key, Object defaultObject) {
        SharedPreferences sp = MyApplication.Companion.getInstance().getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);

        if (defaultObject instanceof String) {
            return sp.getString(key, (String) defaultObject);
        } else if (defaultObject instanceof Integer) {
            return sp.getInt(key, (Integer) defaultObject);
        } else if (defaultObject instanceof Boolean) {
            return sp.getBoolean(key, (Boolean) defaultObject);
        } else if (defaultObject instanceof Float) {
            return sp.getFloat(key, (Float) defaultObject);
        } else if (defaultObject instanceof Long) {
            return sp.getLong(key, (Long) defaultObject);
        }

        return null;
    }

    /**
     * 92      * 移除某个 key 值已经对应的值
     * 93      *
     * 94      * @param context
     * 95      * @param key
     * 96
     */
    public static void remove(String key) {
        SharedPreferences sp = MyApplication.Companion.getInstance().getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        SharedPreferencesCompat.apply(editor);
    }

    /**
     * 107      * 清除所有数据
     * 108      *
     * 109      * @param context
     * 110
     */
    public static void clear() {
        SharedPreferences sp = MyApplication.Companion.getInstance().getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        SharedPreferencesCompat.apply(editor);
    }

    /**
     * 121      * 查询某个 key 是否已经存在
     * 122      *
     * 123      * @param context
     * 124      * @param key
     * 125      * @return
     * 126
     */
    public static boolean contains(String key) {
        SharedPreferences sp = MyApplication.Companion.getInstance().getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        return sp.contains(key);
    }

    /**
     * 135      * 返回所有的键值对
     * 136      *
     * 137      * @param context
     * 138      * @return
     * 139
     */
    public static Map<String, ?> getAll() {
        SharedPreferences sp = MyApplication.Companion.getInstance().getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        return sp.getAll();
    }

    /**
     * 148      * 创建一个解决 SharedPreferencesCompat.apply 方法的一个兼容类
     * 149      *
     * 150      * @author zhy
     * 151      *
     * 152
     */
    private static class SharedPreferencesCompat {
        private static final Method sApplyMethod = findApplyMethod();

        /**
         * 158          * 反射查找 apply 的方法
         * 159          *
         * 160          * @return
         * 161
         */
        @SuppressWarnings({"unchecked", "rawtypes"})
        private static Method findApplyMethod() {
            try {
                Class clz = SharedPreferences.Editor.class;
                return clz.getMethod("apply");
            } catch (NoSuchMethodException e) {
            }

            return null;
        }

        /**
         * 177          * 如果找到则使用 apply 执行，否则使用 commit
         * 178          *
         * 179          * @param editor
         * 180
         */
        public static void apply(SharedPreferences.Editor editor) {
            try {
                if (sApplyMethod != null) {
                    sApplyMethod.invoke(editor);
                    return;
                }
            } catch (IllegalArgumentException e) {
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
            editor.commit();
        }
    }

}
